public class CreatTreeByInorderAndPostorder {//根据后序和中序创建二叉树
    public class TreeNode {
        public int val;
        public TreeNode right;
        public TreeNode left;

        public TreeNode(int val) {

            this.val = val;
        }
    }
    public int postindex;
    public TreeNode buildTree(int[] inorder, int[] postorder) {
        postindex = postorder.length-1;
        return buildTreeN(inorder,postorder,0,postorder.length-1);
    }
    public TreeNode buildTreeN(int[] inorder, int[] postorder,int inBegen,int inEnd){
        if(inBegen>inEnd){//该情况下 表明 root没有子树了
            return null;
        }
        TreeNode root = new TreeNode(postorder[postindex]);
        int rootIndex = findVal(inorder,inBegen,inEnd,postorder[postindex]);
        postindex--;
        root.right = buildTreeN(inorder,postorder,rootIndex+1,inEnd);
        root.left = buildTreeN(inorder,postorder,inBegen,rootIndex-1);
        return root;
    }
    public int findVal(int[] inorder,int inBegen,int inEnd,int val){
        for(int i = inBegen;i<=inEnd;i++){
            if(inorder[i] == val){
                return i;
            }
        }
        return -1;
    }
}